<?php
    require_once('popup_base_jectly.php');

	/*
     * sub classes of page_base_jectly will include only the changes from index_2;
	 */
	class popup_request_complete extends popup_base_jectly {

        protected $contents_html='<div class=\'center\'>This is popup_request_complete page.</div>';

        protected $id;//project_id
        public $project;
        protected $request_id;//request_id
        public $request;//TableProjectRequestComplete
        
        public $bool_confirming=false;
        public $bool_message_exist=false;
        public $bool_error=false;
        public $bool_permitted=false;
        public $textbox_comment;
        public $lstcomplete_type;
        public $button_back;
        public $button_ok;
        
		public $button_confirm;
        public $button_cancel;
        public $uid;
        public $target_user;
         
        public $str_message;
        public $str_permission_error_text='no permission';
		   public $str_confirming_text='Are you sure to do this?<br /><br />';
        
		
        
       /* public $str_confirming_text='<br/><br/>Please confirm to <strong>complete</strong> project.<br /><br />';
        
        public $str_confirming_text_user="<br/><br/>You <strong>have to communicate </strong> with the buyer before requesting to complete.<br /><br />";
        
         public $str_confirming_text_user1='<br/><br/>If your request is accepted, this project will be <strong>completed </strong>with success and you can not communicate with the buyer no more throught this project.<br /><br />';

*/
        function __construct() {
            parent::__construct();
        }

        protected function Form_Create() {

            $this->check_query_string();

            $this->load_project();
            $this->load_target_user();
            
                 	if($this->bool_popup_request_complete_permitted())
            {
                $this->bool_permitted=true;
            }
			
			
			  if($this->bool_permitted)
            {
                
                $this->button_confirm_Create();
                $this->button_cancel_Create();
                $this->textbox_comment_Create();
                $this->lstcomplete_type_Create();
                $this->button_back_Create();
                $this->button_ok_Create();
                
				}
		}
	 	 protected function textbox_comment_Create() {
            $this->textbox_comment = new QTextBox($this);
            $this->textbox_comment->Width=400;
            $this->textbox_comment->Columns=50;
            $this->textbox_comment->Rows=10;
            $this->textbox_comment->Name = 'comment';
            //$this->textbox_comment->CssClass = 'input';
            $this->textbox_comment->Visible=true;
            $this->textbox_comment->Wrap=true;
            $this->textbox_comment->TextMode=QTextMode::MultiLine;
            $this->textbox_comment->Overflow=QOverflow::Auto;
            if($this->bool_confirming)$this->textbox_comment->Visible=false;
        }
        
        protected function lstcomplete_type_Create() {
        	
        	$this->lstcomplete_type= new QListBox($this);
        	
        	$this->lstcomplete_type->AddItem('--Select One--',-1);
        	$this->lstcomplete_type->AddItem('Mutally Agree',0);
        	$this->lstcomplete_type->AddItem('MisMatched',1);
        	$this->lstcomplete_type->AddItem('No Communication',2); 
        	$this->lstcomplete_type->AddItem('Delaying',3);	
            $this->lstcomplete_type->AddItem('Other',4);    
             if($this->bool_confirming)$this->lstcomplete_type->Visible=false;
        }
		
		 protected function button_confirm_Create() {
            $this->button_confirm = new QButton($this);
            $this->button_confirm->Text = 'confirm';
            $this->button_confirm->AddAction(new QClickEvent(), new QServerAction('confirm_Click'));
            $this->button_confirm->PrimaryButton = true;
        }
	   protected function button_cancel_Create() {
            $this->button_cancel = new QButton($this);
            $this->button_cancel->Text = 'cancel';
            $this->button_cancel->AddAction(new QClickEvent(), new QServerAction('cancel_Click'));
            //$this->button_cancel->PrimaryButton = true;
        }
        protected function button_back_Create() {
            $this->button_back = new QButton($this);
            $this->button_back->Text = 'back';
            $this->button_back->AddAction(new QClickEvent(), new QServerAction('back_Click'));
            //$this->button_back->PrimaryButton = true;
        }

        protected function button_ok_Create() {
            $this->button_ok = new QButton($this);
            $this->button_ok->Text = 'ok';
            $this->button_ok->AddAction(new QClickEvent(), new QServerAction('ok_Click'));
            //$this->button_ok->PrimaryButton = true;
        }
		   public function confirm_Click($strFormId, $strControlId, $strParameter) {
		   	   $this->bool_confirming=false;
            $this->bool_error=false;

             if($this->bool_qcontrol_text_input_ok($this->textbox_comment))
            {
                //$this->append_line_confirming_text('textbox_delivery_within input='.$this->textbox_delivery_within->Text);
                //$this->bool_error=true;
                
                if($this->bool_qcontrol_text_input_within($this->textbox_comment,200) && $this->bool_qcontrol_text_input_over($this->textbox_comment,6))
                {
                    
                }
                else
                {
                    $this->textbox_comment->Blink();
                    $this->set_error_message('Please input your textbox_comment within the range(6-365)');
                }
            }
           if($this->bool_qcontrol_text_input_ok($this->lstcomplete_type))
           {
           	if($this->bool_qcontrol_listbox_validate($this->lstcomplete_type))
           	{}
           	else 
           	{
           		$this->lstcomplete_type->Blink();
           		$this->set_error_message('Please select Reason Type');
           	}
           }
           

            //$this->reset_message();
            //$this->append_line_message('confirming');
            //$this->bool_message_exist=true;

            if(!$this->bool_error)
            {
             	  $this->append_line_confirming_text('Reason Type='.$this->lstcomplete_type->SelectedName);	  
                $this->append_line_confirming_text('comment='.$this->textbox_comment->Text);

                $this->bool_confirming=true;
                $this->bool_message_exist=false;
            }
            else
            {
                $this->bool_message_exist=true;
            }	
		 
		   }
		   
        public function ok_Click($strFormId, $strControlId, $strParameter) {

            //save data
    
            $objUser = unserialize($_SESSION['User']);
            $this->UserId=$objUser->Id;

            $request= new TableProjectRequestComplete();
			$request->BuyerId=$this->project->UserId;
			$request->ProviderId=$this->uid;
			$request->CompleteType=$this->lstcomplete_type->SelectedValue;
			$request->ProjectId=$this->id;
			$request->Message=$this->textbox_comment->Text;
            $request->set_status_created();
			$request->set_request_date_for_now();
            $request->Save();

            QApplication::ExecuteJavaScript('opener.location.reload(true);self.window.close();');

        }

		  public function back_Click($strFormId, $strControlId, $strParameter) {
            $this->bool_confirming=false;
            $this->bool_message_exist=false;
        } 	
		   	
		   	
		   	
         
		   	
		  
          public function cancel_Click($strFormId, $strControlId, $strParameter) {
            QApplication::ExecuteJavaScript('self.window.close();');
        }
		protected function load_target_user()
        {
            if(isset($this->uid) && is_numeric($this->uid))
            {
                $this->target_user=TableUserProject::LoadById($this->uid);

                if(isset($this->target_user))
                {
                    $this->contents_html.=' target_user loaded-['.$this->target_user->UserName.'] user_id=['.$this->target_user->Id.']';
                }
                else
                {
                    $this->contents_html.=' target_user not found id=['.$this->uid.']';
                    $this->uid=null;
                    $this->target_user=null;
                }
            }
        }

        protected function check_query_string()
        {
            if(is_numeric(QApplication::QueryString('id')))
            {
                $this->id=QApplication::QueryString('id');//project_id
            }

            if(is_numeric(QApplication::QueryString('rid')))
            {
                $this->request_id=QApplication::QueryString('rid');//request_id
            }
        }

        protected function load_project()
        {
            if(isset($this->id) && is_numeric($this->id))
            {
                $this->project=TableProject::LoadById($this->id);
                $objUser = unserialize($_SESSION['User']);
            	$this->UserId=$objUser->Id;
            	
            	$this->request_id=TableProjectRequestComplete::load_request_id_for_project($this->id);
            	$this->request=TableProjectRequestComplete::LoadById($this->request_id);
            	$this->uid=TableProjectWon::Won_Provider_id_after_starting($this->id);
            	
            	
            	
                if(isset($this->project))
                {
                    $this->contents_html='project loaded-['.$this->project->Title.'] provider_id=['.$this->project->UserId.'] buyer_id=['.$this->UserId.']';
                }
                else
                {
                    $this->contents_html='project not found id=['.$this->id.']';
                    $this->id=null;
                    $this->project=null;
                }
            }
        }

        protected function load_request()
        {
            if(isset($this->request_id) && is_numeric($this->request_id))
            {
                $this->request=TableProjectRequestComplete::LoadById($this->request_id);

                if(isset($this->request))
                {
                    $this->contents_html.=' request loaded-['.$this->request->Id.']';
                }
                else
                {
                    $this->contents_html.=' request not found id=['.$this->request_id.']';
                    $this->request_id=null;
                    $this->request=null;
                }
            }
        }
         //check permissions
        public function bool_project_loaded()
        {
            return isset($this->project);
        }

        public function bool_request_loaded()
        {
            return isset($this->request);
        }

        public function bool_my_project()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                return $this->project->bool_my_project($this->UserId);
            }

            return false;
        }

        public function bool_won_project()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                return $this->project->bool_won_project($this->UserId);
            }

            return false;
        }
        
        public function bool_bid_exist()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                if($this->project->count_bid_for_this_project()>0)
                {
                    return true;
                }
            }

            return false;
        }
         public function bool_request_exist()
        {
            if(isset($this->request) && isset($this->UserId))
            {
                if($this->request->count_bid_for_this_project()>0)
                {
                    return true;
                }
            }

            return false;
        }

        public function bool_already_bidding()
        {
            return false;
        }

        public function bool_can_bid()
        {
            return false;
        }

        public function bool_project_open()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                return $this->project->bool_open_project();
            }

            return false;
        }

        public function bool_project_after_picked()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                return $this->project->bool_after_picked_project();
            }

            return false;
        }

        public function bool_project_bidding_end()
        {
            if(!$this->bool_project_open() && !$this->bool_project_after_picked())return true;

            return false;
        }
          public function bool_project_request_complete_accept()
        {
            if(isset($this->request) && isset($this->UserId))
            {
                if($this->request->count_bid_for_acccep_complete()>0)
                {
                    return true;
                }
            }

            return false;
        }
   

        protected function bool_popup_request_complete_permitted()
        {
           if(!$this->bool_logined()){
                $this->reset_permission_error_text();
                $this->append_line_permission_error_text('You must login to access this page at least');
                return false;
            }
             
            if(!$this->bool_project_loaded()){
                $this->reset_permission_error_text();
                $this->append_line_permission_error_text('Project not found');
                return false;
            }
            if($this->bool_my_project()){
                $this->reset_permission_error_text();
                $this->append_line_permission_error_text('The project is yours');
                return false;
            }
           
            if(!$this->bool_project_open()){
                $this->reset_permission_error_text();
                $this->append_line_permission_error_text('The project is closed for bidding');
                return false;
            }
              if(!$this->bool_won_project()){
                $this->reset_permission_error_text();
                $this->append_line_permission_error_text('The project has has not been started');
                return false;
            }
              
             if($this-> bool_project_request_complete_accept()){
                $this->reset_permission_error_text();
                $this->append_line_permission_error_text('The project has been made already been accepted');
                return false;
            }
            return true;
        }

        protected function bool_status()
	{
		if(isset($this->project)&& isset($this->UserId) && isset($this->request))
		{
			return $this->request->bool_created();
		}
		return false;

	}

       public function render_permission_error_text() {

            if(!$this->bool_permitted)
            {
                echo($this->str_permission_error_text);
            }
        }
        
        public function render_message_text() {

            if($this->bool_message_exist)
            {
                echo($this->str_message);
            }
        }

        public function render_confirming_text() {

            if($this->bool_confirming)
            {
                echo($this->str_confirming_text);
            }
        }

        public function set_error_message($str_) {

            if(!$this->bool_error)
            {
                $this->reset_message();
                $this->append_line_message($str_);
                $this->bool_error=true;
            }
        }

        public function reset_error_message() {
            $this->reset_message();
            $this->bool_error=false;
            $this->bool_message_exist=false;
        }

        public function append_permission_error_text($str_) {

            $this->str_permission_error_text.=$str_;
        }

        public function append_line_permission_error_text($str_) {

            $this->append_permission_error_text($str_.'<br />');
        }

        public function reset_permission_error_text() {

            $this->str_permission_error_text='';
        }

        public function append_message($str_) {

            $this->str_message.=$str_;
        }

        public function append_line_message($str_) {

            $this->append_message($str_.'<br />');
        }

        public function reset_message() {

            $this->str_message='';
        }

        public function append_confirming_text($str_) {

            $this->str_confirming_text.=$str_;
        }

        public function append_line_confirming_text($str_) {

            $this->append_confirming_text($str_.'<br />');
        }

        public function reset_confirming_text() {

            $this->str_confirming_text='';
        }

        public function bool_qcontrol_text_input_ok($qcontrol_) {

            if(isset($qcontrol_))
            {
                if (!($qcontrol_ instanceof QTextBoxBase))return false;

                if(strlen(trim($qcontrol_->Text))>0)
                {
                    $qcontrol_->Text=trim($qcontrol_->Text);
                    return true;
                }
            }
            return false;
        }
           public function bool_qcontrol_listbox_validate($qcontrol_) {

            if(isset($qcontrol_))
            {
                if (!($qcontrol_ instanceof QListBoxBase  ))return false;

                if($qcontrol_->SelectedValue != -1)
                {
                    //$qcontrol_->Text=trim($qcontrol_->Text);
                    return true;
                }
            }
            return false;
        }


        public function bool_qcontrol_text_input_within($qcontrol_,$limit_) {

            if(isset($qcontrol_))
            {
                if (!($qcontrol_ instanceof QTextBoxBase))return false;

                if(strlen($qcontrol_->Text)<=$limit_)
                {
                    //$qcontrol_->Text=trim($qcontrol_->Text);
                    return true;
                }
            }
            return false;
        }

        public function bool_qcontrol_text_input_over($qcontrol_,$limit_) {

            if(isset($qcontrol_))
            {
                if (!($qcontrol_ instanceof QTextBoxBase))return false;

                if(strlen($qcontrol_->Text)>=$limit_)
                {
                    //$qcontrol_->Text=trim($qcontrol_->Text);
                    return true;
                }
            }
            return false;
        }
 public function render_contents_area() {

            if($this->bool_contents_area_on)
            {
                echo($this->contents_html);
            }
        }

	}

    popup_request_complete::Run('popup_request_complete','popup_request_complete.tpl.php');

?>